
IaC(Infrastructure as Code)는 인프라를 코드로 정의하고 관리하는 방법론이다. 이를 통해 인프라를 일관되게 배포하고 관리할 수 있으며, 버전 관리와 협업이 용이하다.
CloudFormation은 AWS 인프라의 모든 리소스를 선언적으로 정의할 수 있는 서비스다. 코드로 인프라를 정의하여 자동으로 리소스를 생성하고 관리할 수 있다.
아래는 CloudFormation을 사용하여 S3 버킷과 EC2 인스턴스를 생성하는 예시 템플릿이다.
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyS3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: 'my-cloudformation-bucket'
MyEC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: 't2.micro'
ImageId: 'ami-0c55b159cbfafe1f0'
KeyName: 'my-key-pair'
SecurityGroups:
- !Ref MySecurityGroup
MySecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'Allow SSH access'
SecurityGroupIngress:
- IpProtocol: 'tcp'
FromPort: '22'
ToPort: '22'
CidrIp: '0.0.0.0/0'
AWS CDK는 익숙한 프로그래밍 언어로 클라우드 인프라를 정의할 수 있게 해준다. JavaScript, TypeScript, Python, Java, .NET 등 다양한 언어를 지원하며, 코드로 인프라를 작성하여 CloudFormation 템플릿으로 컴파일된다.
from aws_cdk import (
aws_s3 as s3,
aws_lambda as _lambda,
core,
)
class MyStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# S3 Bucket
bucket = s3.Bucket(self,
"MyBucket",
bucket_name="my-cdk-bucket"
)
# Lambda Function
function = _lambda.Function(self,
"MyFunction",
runtime=_lambda.Runtime.PYTHON_3_8,
handler="index.handler",
code=_lambda.Code.from_asset("lambda")
)
app = core.App()
MyStack(app, "MyStack")
app.synth()
Terraform은 HashiCorp에서 개발한 IaC 도구로, AWS뿐만 아니라 다양한 클라우드 제공업체에서 인프라를 코드로 관리할 수 있다.
provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-terraform-bucket"
}
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = "my-key-pair"
tags = {
Name = "MyInstance"
}
}